import request from '@/libs/request';

/**
 * @description NFC营销活动管理 -- 获取活动列表
 * @param {Object} params 查询参数
 */
export function getCampaignList(params) {
  return request({
    url: 'nfc/campaigns',
    method: 'get',
    params,
  });
}

/**
 * @description NFC营销活动管理 -- 获取活动详情
 * @param {Number} id 活动ID
 */
export function getCampaignInfo(id) {
  return request({
    url: 'nfc/campaigns/info',
    method: 'get',
    params: { id },
  });
}

/**
 * @description NFC营销活动管理 -- 创建活动
 * @param {Object} data 活动数据
 */
export function createCampaign(data) {
  return request({
    url: 'nfc/campaigns/edit',
    method: 'post',
    data,
  });
}

/**
 * @description NFC营销活动管理 -- 编辑活动
 * @param {Object} data 活动数据
 */
export function updateCampaign(data) {
  return request({
    url: 'nfc/campaigns/edit',
    method: 'post',
    data,
  });
}

/**
 * @description NFC营销活动管理 -- 删除活动
 * @param {Number} id 活动ID
 */
export function deleteCampaign(id) {
  return request({
    url: 'nfc/campaigns/delete',
    method: 'post',
    data: { id },
  });
}

/**
 * @description NFC营销活动管理 -- 设置活动状态
 * @param {Number} id 活动ID
 * @param {Number} status 状态值 (0:禁用, 1:启用)
 */
export function setCampaignStatus(id, status) {
  return request({
    url: 'nfc/campaigns/status',
    method: 'post',
    data: { id, status },
  });
}

/**
 * @description NFC营销活动管理 -- 生成NFC链接
 * @param {Number} id 活动ID
 */
export function generateNfcLink(id) {
  return request({
    url: 'nfc/campaigns/generate-link',
    method: 'post',
    data: { id },
  });
}

/**
 * @description NFC营销活动管理 -- 获取商户列表
 */
export function getMerchantList() {
  return request({
    url: 'nfc/campaigns/merchants',
    method: 'get',
  });
}

/**
 * @description NFC营销活动管理 -- 远程搜索商户
 * @param {Object} params 查询参数 {keyword, page, limit}
 */
export function searchMerchants(params) {
  return request({
    url: 'nfc/campaigns/merchants/search',
    method: 'get',
    params,
  });
}

/**
 * @description NFC营销活动管理 -- 批量设置状态
 * @param {String} ids 活动ID列表（逗号分隔）
 * @param {Number} status 状态值 (0:禁用, 1:启用)
 */
export function batchSetCampaignStatus(ids, status) {
  return request({
    url: 'nfc/campaigns/batch-status',
    method: 'post',
    data: { ids, status },
  });
}

// ============ 优惠券核销相关API ============

/**
 * @description 验证优惠券
 * @param {Object} data 验证参数 {coupon_code, merchant_id}
 */
export function verifyCoupon(data) {
  return request({
    url: 'nfc/coupon/verify',
    method: 'post',
    data,
  });
}

/**
 * @description 核销优惠券
 * @param {Object} data 核销参数
 */
export function consumeCoupon(data) {
  return request({
    url: 'nfc/coupon/consume',
    method: 'post',
    data,
  });
}

/**
 * @description 获取优惠券详情
 * @param {String} couponCode 优惠券代码
 * @param {Number} merchantId 商户ID
 */
export function getCouponDetail(couponCode, merchantId) {
  return request({
    url: `nfc/coupon/detail/${couponCode}`,
    method: 'get',
    params: { merchant_id: merchantId },
  });
}

/**
 * @description 查询核销记录
 * @param {Object} params 查询参数
 */
export function getConsumeRecords(params) {
  return request({
    url: 'nfc/coupon/consume/records',
    method: 'get',
    params,
  });
}

/**
 * @description 获取核销统计
 * @param {Object} params 查询参数
 */
export function getConsumeStats(params) {
  return request({
    url: 'nfc/coupon/consume/stats',
    method: 'get',
    params,
  });
}

/**
 * @description 获取核销趋势
 * @param {Object} params 查询参数
 */
export function getConsumeTrend(params) {
  return request({
    url: 'nfc/coupon/consume/trend',
    method: 'get',
    params,
  });
}

/**
 * @description 批量验证优惠券
 * @param {Object} data 批量验证参数 {coupon_codes, merchant_id}
 */
export function batchVerifyCoupons(data) {
  return request({
    url: 'nfc/coupon/batch-verify',
    method: 'post',
    data,
  });
}

/**
 * @description 撤销核销
 * @param {Object} data 撤销参数 {record_id, admin_id, revoke_reason}
 */
export function revokeCouponConsume(data) {
  return request({
    url: 'nfc/coupon/consume/revoke',
    method: 'post',
    data,
  });
}

/**
 * @description 导出核销记录
 * @param {Object} params 导出参数
 */
export function exportConsumeRecords(params) {
  return request({
    url: 'nfc/coupon/consume/export',
    method: 'get',
    params,
    responseType: 'blob',
  });
}

/**
 * @description 获取核销记录详情
 * @param {Number} id 记录ID
 */
export function getConsumeRecordDetail(id) {
  return request({
    url: `nfc/coupon/consume/record/${id}`,
    method: 'get',
  });
}

// 生成优惠券二维码
export function generateCouponQrCode(data) {
  return request.post('/nfc/coupon/qrcode/generate', data);
}

// 批量生成优惠券二维码
export function batchGenerateCouponQrCodes(data) {
  return request.post('/nfc/coupon/qrcode/batch', data);
}

// 解析二维码数据
export function parseCouponQrCode(data) {
  return request.post('/nfc/coupon/qrcode/parse', data);
}

// ============ NFC保证金管理相关API ============

/**
 * @description 获取保证金账户列表
 * @param {Object} params 查询参数
 */
export function getDepositList(params) {
  return request({
    url: 'nfc/deposit/list',
    method: 'get',
    params,
  });
}

/**
 * @description 获取保证金账户详情
 * @param {Number} id 账户ID
 */
export function getDepositInfo(id) {
  return request({
    url: `nfc/deposit/info/${id}`,
    method: 'get',
  });
}

/**
 * @description 支付保证金
 * @param {Object} data 支付参数
 */
export function payDeposit(data) {
  return request({
    url: 'nfc/deposit/pay',
    method: 'post',
    data,
  });
}

/**
 * @description 退还保证金
 * @param {Object} data 退还参数
 */
export function refundDeposit(data) {
  return request({
    url: 'nfc/deposit/refund',
    method: 'post',
    data,
  });
}

/**
 * @description 冻结保证金
 * @param {Object} data 冻结参数
 */
export function freezeDeposit(data) {
  return request({
    url: 'nfc/deposit/freeze',
    method: 'post',
    data,
  });
}

/**
 * @description 解冻保证金
 * @param {Object} data 解冻参数
 */
export function unfreezeDeposit(data) {
  return request({
    url: 'nfc/deposit/unfreeze',
    method: 'post',
    data,
  });
}

/**
 * @description 获取保证金交易记录
 * @param {Object} params 查询参数
 */
export function getDepositRecords(params) {
  return request({
    url: 'nfc/deposit/records',
    method: 'get',
    params,
  });
}

/**
 * @description 批量处理保证金
 * @param {Object} data 批量操作参数
 */
export function batchProcessDeposits(data) {
  return request({
    url: 'nfc/deposit/batch',
    method: 'post',
    data,
  });
}

/**
 * @description 导出保证金数据
 * @param {Object} params 导出参数
 */
export function exportDepositData(params) {
  return request({
    url: 'nfc/deposit/export',
    method: 'get',
    params,
    responseType: 'blob',
  });
}

/**
 * @description 获取保证金统计数据
 * @param {Object} params 查询参数
 */
export function getDepositStats(params) {
  return request({
    url: 'nfc/deposit/stats',
    method: 'get',
    params,
  });
}

// ============ NFC佣金管理相关API ============

/**
 * @description 获取佣金记录
 * @param {Object} params 查询参数
 */
export function getCommissionRecords(params) {
  return request({
    url: 'nfc/commission/records',
    method: 'get',
    params,
  });
}

/**
 * @description 获取结算列表
 * @param {Object} params 查询参数
 */
export function getSettlements(params) {
  return request({
    url: 'nfc/commission/settlements',
    method: 'get',
    params,
  });
}

/**
 * @description 创建结算单
 * @param {Object} data 创建参数
 */
export function createSettlement(data) {
  return request({
    url: 'nfc/commission/settlement/create',
    method: 'post',
    data,
  });
}

/**
 * @description 处理结算
 * @param {Object} data 处理参数
 */
export function processSettlement(data) {
  return request({
    url: 'nfc/commission/settlement/process',
    method: 'post',
    data,
  });
}

/**
 * @description 完成结算
 * @param {Object} data 完成参数
 */
export function completeSettlement(data) {
  return request({
    url: 'nfc/commission/settlement/complete',
    method: 'post',
    data,
  });
}

/**
 * @description 取消结算
 * @param {Object} data 取消参数
 */
export function cancelSettlement(data) {
  return request({
    url: 'nfc/commission/settlement/cancel',
    method: 'post',
    data,
  });
}

/**
 * @description 获取结算详情
 * @param {Number} id 结算单ID
 */
export function getSettlementDetails(id) {
  return request({
    url: `nfc/commission/settlement/details/${id}`,
    method: 'get',
  });
}

/**
 * @description 批量更新佣金
 * @param {Object} data 批量更新参数
 */
export function batchUpdateCommissions(data) {
  return request({
    url: 'nfc/commission/batch-update',
    method: 'post',
    data,
  });
}

/**
 * @description 导出佣金数据
 * @param {Object} params 导出参数
 */
export function exportCommissionData(params) {
  return request({
    url: 'nfc/commission/export',
    method: 'get',
    params,
    responseType: 'blob',
  });
}

/**
 * @description 获取佣金统计数据
 * @param {Object} params 查询参数
 */
export function getCommissionStats(params) {
  return request({
    url: 'nfc/commission/stats',
    method: 'get',
    params,
  });
}

// ============ NFC积分配置管理相关API ============

/**
 * @description 获取积分配置列表
 * @param {Object} params 查询参数
 */
export function getPointsConfigList(params) {
  return request({
    url: 'nfc/points/config/list',
    method: 'get',
    params,
  });
}

/**
 * @description 获取积分配置详情
 * @param {Number} id 配置ID
 */
export function getPointsConfigInfo(id) {
  return request({
    url: `nfc/points/config/info/${id}`,
    method: 'get',
  });
}

/**
 * @description 保存积分配置
 * @param {Object} data 配置数据
 */
export function savePointsConfig(data) {
  return request({
    url: 'nfc/points/config/save',
    method: 'post',
    data,
  });
}

/**
 * @description 删除积分配置
 * @param {Object} data 删除参数
 */
export function deletePointsConfig(data) {
  return request({
    url: 'nfc/points/config/delete',
    method: 'post',
    data,
  });
}

/**
 * @description 获取有效配置
 * @param {Object} params 查询参数
 */
export function getEffectivePointsConfig(params) {
  return request({
    url: 'nfc/points/config/effective',
    method: 'get',
    params,
  });
}

/**
 * @description 复制配置
 * @param {Object} data 复制参数
 */
export function copyPointsConfig(data) {
  return request({
    url: 'nfc/points/config/copy',
    method: 'post',
    data,
  });
}

/**
 * @description 验证配置规则
 * @param {Object} data 验证参数
 */
export function validatePointsConfigRules(data) {
  return request({
    url: 'nfc/points/config/validate',
    method: 'post',
    data,
  });
}

/**
 * @description 批量更新配置
 * @param {Object} data 批量更新参数
 */
export function batchUpdatePointsConfigs(data) {
  return request({
    url: 'nfc/points/config/batch-update',
    method: 'post',
    data,
  });
}

/**
 * @description 获取配置模板
 * @param {Object} params 查询参数
 */
export function getPointsConfigTemplates(params) {
  return request({
    url: 'nfc/points/config/templates',
    method: 'get',
    params,
  });
}

/**
 * @description 导入配置（JSON提交）
 * @param {Object} data { config_data, import_mode, conflict_strategy }
 */
export function importPointsConfig(data) {
  return request({
    url: 'nfc/points/config/import',
    method: 'post',
    data,
  });
}

/**
 * @description 导出配置数据
 * @param {Object} params 导出参数
 */
export function exportPointsConfig(params) {
  return request({
    url: 'nfc/points/config/export',
    method: 'get',
    params,
    responseType: 'blob',
    timeout: 30000, // 30秒超时
  });
}

/**
 * @description 下载配置模板
 */
export function downloadPointsConfigTemplate() {
  return request({
    url: 'nfc/points/config/template',
    method: 'get',
    responseType: 'blob',
  });
}

// ============ NFC综合报表相关API ============

/**
 * @description 获取积分分析报表
 * @param {Object} params 查询参数
 */
export function getPointsReport(params) {
  return request({
    url: 'nfc/reports/points',
    method: 'get',
    params,
  });
}

/**
 * @description 获取佣金分析报表
 * @param {Object} params 查询参数
 */
export function getCommissionReport(params) {
  return request({
    url: 'nfc/reports/commission',
    method: 'get',
    params,
  });
}

/**
 * @description 获取裂变效果报表
 * @param {Object} params 查询参数
 */
export function getFissionReport(params) {
  return request({
    url: 'nfc/reports/fission',
    method: 'get',
    params,
  });
}

/**
 * @description 获取保证金报表
 * @param {Object} params 查询参数
 */
export function getDepositReport(params) {
  return request({
    url: 'nfc/reports/deposit',
    method: 'get',
    params,
  });
}

/**
 * @description 获取综合概览仪表板
 * @param {Object} params 查询参数
 */
export function getOverviewDashboard(params) {
  return request({
    url: 'nfc/reports/overview',
    method: 'get',
    params,
  });
}

/**
 * @description 获取对比分析
 * @param {Object} params 查询参数
 */
export function getComparativeAnalysis(params) {
  return request({
    url: 'nfc/reports/comparative',
    method: 'get',
    params,
  });
}

/**
 * @description 获取商户绩效分析
 * @param {Object} params 查询参数
 */
export function getMerchantPerformance(params) {
  return request({
    url: 'nfc/reports/merchant-performance',
    method: 'get',
    params,
  });
}

/**
 * @description 获取活动效果分析
 * @param {Object} params 查询参数
 */
export function getCampaignEffectiveness(params) {
  return request({
    url: 'nfc/reports/campaign-effectiveness',
    method: 'get',
    params,
  });
}

/**
 * @description 获取实时数据统计
 * @param {Object} params 查询参数
 */
export function getRealtimeStats(params) {
  return request({
    url: 'nfc/reports/realtime',
    method: 'get',
    params,
  });
}

/**
 * @description 获取自定义报表
 * @param {Object} data 报表配置参数
 */
export function getCustomReport(data) {
  return request({
    url: 'nfc/reports/custom',
    method: 'post',
    data,
  });
}

/**
 * @description 导出综合报表
 * @param {Object} params 导出参数
 */
export function exportReport(params) {
  return request({
    url: 'nfc/reports/export',
    method: 'get',
    params,
    responseType: 'blob',
  });
}

/**
 * @description 获取报表商户列表
 */
export function getReportMerchantList() {
  return request({
    url: 'nfc/reports/merchants',
    method: 'get',
  });
}

/**
 * @description 获取报表活动列表
 */
export function getReportCampaignList() {
  return request({
    url: 'nfc/reports/campaigns',
    method: 'get',
  });
}

// ============ API 分组导出 ============

// 保证金管理API
export const nfcDepositAPI = {
  getList: getDepositList,
  getInfo: getDepositInfo,
  pay: payDeposit,
  refund: refundDeposit,
  freeze: freezeDeposit,
  unfreeze: unfreezeDeposit,
  getRecords: getDepositRecords,
  batchProcess: batchProcessDeposits,
  export: exportDepositData,
  getStats: getDepositStats,
};

// 佣金管理API
export const nfcCommissionAPI = {
  getCommissionRecords,
  getSettlements,
  createSettlement,
  processSettlement,
  completeSettlement,
  cancelSettlement,
  getSettlementDetails,
  batchUpdateCommissions,
  exportCommissionData,
  getCommissionStats,
};

// 积分配置管理API
export const nfcPointsConfigAPI = {
  getConfigList: getPointsConfigList,
  getConfigInfo: getPointsConfigInfo,
  saveConfig: savePointsConfig,
  deleteConfig: deletePointsConfig,
  getEffectiveConfig: getEffectivePointsConfig,
  copyConfig: copyPointsConfig,
  validateConfigRules: validatePointsConfigRules,
  batchUpdateConfigs: batchUpdatePointsConfigs,
  getConfigTemplates: getPointsConfigTemplates,
  importConfig: importPointsConfig,
  exportConfig: exportPointsConfig,
  downloadTemplate: downloadPointsConfigTemplate,
};

// 综合报表API
export const nfcReportsAPI = {
  getPointsReport,
  getCommissionReport,
  getFissionReport,
  getDepositReport,
  getOverviewDashboard,
  getComparativeAnalysis,
  getMerchantPerformance,
  getCampaignEffectiveness,
  getRealtimeStats,
  getCustomReport,
  exportReport,
  getMerchantList: getReportMerchantList,
  getCampaignList: getReportCampaignList,
};